package com.csair.irp.job.file.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * 加减乘除运算
 * 
 * @author Sixg 2017-07-27
 * @version 1.0
 *
 */
public class CalculateUtil {

	private static int defaultScale = 2;

	private static int defaultRound = BigDecimal.ROUND_HALF_UP;
    private static final String minusFlag = "-";

    public static double cal(String calcAmount) {
        if (StringUtils.isEmpty(calcAmount)) {
            return 0.0;
        }
        //是否为负数
        boolean isMinus = calcAmount.contains(minusFlag) ? true : false;
        calcAmount = calcAmount.replace(minusFlag, "");
        double ticketDoumentAmount = Double.valueOf(calcAmount) / 100;
        if (isMinus) {
            ticketDoumentAmount = -ticketDoumentAmount;
        }
        return ticketDoumentAmount;
    }

	/**
	 * 
	 * @description 
	 * @author huangjun
	 * @date 2017/11/10
	 */
	public static double add(double d1, double d2) {
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		BigDecimal result = add(b1, b2);
		return result.doubleValue();
	}

	/**
	 * 计算两个数的和
	 * 
	 * @param b1
	 * @param b2
	 * @return b1+b2
	 */
	public static BigDecimal add(BigDecimal b1, BigDecimal b2) {
		if (b1 == null && b2 == null) {
			return new BigDecimal("0");
		} else if (b1 == null && b2 != null) {
			return b2;
		} else if (b1 != null && b2 == null) {
			return b1;
		} else {
			return b1.add(b2);
		}
	}
	
	/**
	 * 
	 * (计算多少数的和)
	 * com.csair.irp.util
	 * methodName：multipleAdd
	 * creater：zwm 
	 * createTime：2017年8月22日-下午6:51:28 
	 * @param list
	 * @return BigDecimal
	 * @exception 
	 * @since  1.0.0
	 */
	public static BigDecimal multipleAdd(BigDecimal... list){
		BigDecimal sum = new BigDecimal(0);
		  for (BigDecimal item:list){
			  if (item==null) {
				  continue;
			    }else{
			    	 sum=sum.add(item);
			    }
			  }
		return sum;
	}
	/**
	 * 计算两个数的差
	 * 
	 * @param b1
	 * @param b2
	 * @return b1-b2
	 */
	public static BigDecimal subtract(BigDecimal b1, BigDecimal b2) {
		if (b1 == null && b2 == null) {
			return new BigDecimal("0");
		} else if (b1 == null && b2 != null) {
			return new BigDecimal("0").subtract(b2);
		} else if (b1 != null && b2 == null) {
			return b1.subtract(new BigDecimal("0"));
		} else {
			return b1.subtract(b2);
		}
	}

	/**
	 * 计算两个数的积
	 * 
	 * @param b1
	 * @param b2
	 * @return b1 * b2
	 */
	public static BigDecimal multiply(BigDecimal b1, BigDecimal b2) {
		if (b1 == null && b2 == null) {
			return new BigDecimal("0");
		} else if (b1 == null && b2 != null) {
			return new BigDecimal("0").multiply(b2);
		} else if (b1 != null && b2 == null) {
			return b1.multiply(new BigDecimal("0"));
		} else {
			return b1.multiply(b2);
		}
	}

	/**
	 * 计算两个数的商
	 * 
	 * @param b1
	 * @param b2
	 * @return b1 除以 b2 默认精度为2
	 */
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2) {
		return divide(b1, b2, defaultScale, defaultRound);
	}

	/**
	 * 计算两个数的商
	 * 
	 * @param b1
	 * @param b2
	 * @param scale
	 *            精度
	 * @return b1 除以 b2
	 */
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2, int scale) {
		return divide(b1, b2, scale, defaultRound);
	}

	/**
	 * b1 除以 b2
	 * 
	 * <pre>
	 * 	ROUND_CEILING:   舍位时往正无穷方向移动   1.1-> 2   1.5-> 2   1.8-> 2   -1.1-> -1   -1.5-> -1   -1.8-> -1 
		ROUND_DOWN:向0的方向移动1.1-> 1   1.5-> 1   1.8-> 1   -1.1-> -1   -1.5-> -1   -1.8> -1 
		ROUND_FLOOR:与CEILING相反，往负无穷   1.1-> 1   1.5-> 1   1.8-> 1   -1.1-> -2   -1.5-> -2   -1.8-> -2 
		ROUND_HALF_DOWN:以5为分界线，或曰五舍六入1.5-> 1   1.6-> 1   -1.5-> -1   -1.6-> -2   
		ROUND_HALF_EVEN:同样以5为分界线，如果是5，则前一位变偶数1.15-> 1.2   1.16-> 1.2   1.25-> 1.2   1.26-> 1.3 
		ROUND_HALF_UP:最常见的四舍五入 
		UNNECESSARY:无需舍位 
		ROUND_UP:与ROUND_DOWN，远离0的方向1.1-> 2   1.5-> 2   1.8-> 2   -1.1-> -2   -1.5-> -2   -1.8-> -2
	 * </pre>
	 * 
	 * @param b1
	 * @param b2
	 * @param scale
	 *            精度
	 * @param roundType
	 *            舍入方式
	 *            <ul>
	 *            <li>BigDecimal.ROUND_UP</li>
	 *            <li>BigDecimal.ROUND_DOWN</li>
	 *            <li>BigDecimal.ROUND_CEILING</li>
	 *            <li>BigDecimal.ROUND_FLOOR</li>
	 *            <li>BigDecimal.ROUND_HALF_UP</li>
	 *            <li>BigDecimal.ROUND_HALF_DOWN</li>
	 *            <li>BigDecimal.ROUND_HALF_EVEN</li>
	 *            <li>BigDecimal.ROUND_UNNECESSARY</li>
	 *            </ul>
	 * @return
	 */
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2, int scale, int roundType) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		if (roundType != BigDecimal.ROUND_UP && roundType != BigDecimal.ROUND_DOWN
				&& roundType != BigDecimal.ROUND_CEILING && roundType != BigDecimal.ROUND_FLOOR
				&& roundType != BigDecimal.ROUND_HALF_UP && roundType != BigDecimal.ROUND_HALF_DOWN
				&& roundType != BigDecimal.ROUND_HALF_EVEN && roundType != BigDecimal.ROUND_UNNECESSARY) {
			throw new IllegalArgumentException(
					"The roundType must be in BigDecimal.ROUND_UP,BigDecimal.ROUND_DOWN,BigDecimal.ROUND_CEILING,"
							+ "BigDecimal.ROUND_FLOOR,BigDecimal.ROUND_HALF_UP,BigDecimal.ROUND_HALF_DOWN,"
							+ "BigDecimal.ROUND_HALF_EVEN,BigDecimal.ROUND_UNNECESSARY");
		}
		if (b1 == null && b2 == null) {
			return new BigDecimal("0");
		} else if (b1 == null && b2 != null) {
			// 0 除以任何数都得零
			return new BigDecimal("0");
		} else if (b1 != null && b2 == null) {
			// 0 不能作为除数，因此用1代替，或者直接返回 b1
			return b1.divide(new BigDecimal("1"));
		} else {
			return b1.divide(b2, scale, roundType);
		}
	}
}
